﻿using System;
using System.Data;
using System.Collections.Generic;
using eTMS.Controllers.ThuTrongTuanService;
namespace eTMS.Controllers{
public class ThuTrongTuanControllerBase : CommonController{
	public ThuTrongTuanControllerBase()
		: base()
	{	
		this.dao = new ThuTrongTuanServiceClient();
	}
	~ThuTrongTuanControllerBase(){
		dao.Close();
	}
	private DataTable _Table;
	private ThuTrongTuanEntity oThuTrongTuan;	
	public ThuTrongTuanEntity ThuTrongTuanObject{
		get{
			return oThuTrongTuan;
		}
		set{
			oThuTrongTuan=value;
		}
	}
	protected ThuTrongTuanServiceClient dao;
	public DataTable Table{
		get{
			this.LoadAllDSThuTrongTuan(false);			
			return _Table;
		}
	}
	public void LoadAllDSThuTrongTuan(bool clearCache){
		bool bForce=clearCache;
		if(_Table==null)
		{
			bForce=true;
		}
		if(!bForce)return;
		DataSet ds=dao.SelectAllDS();
		if(ds.Tables.Count>0){
			_Table=ds.Tables[0];
			this.AddIndexToTable(_Table);
		}
	}	
	public void AddIndexToTable(){
		this.AddIndexToTable(_Table);
	}
	public void ResetTableIndex(){
		this.ResetTableIndex(_Table);
	}
	public bool AddNew(){
		if(oThuTrongTuan==null)return false;
		bool result=true;
		try{
			oThuTrongTuan = dao.SaveRet(oThuTrongTuan);
		}
		catch{
			result=false;
		}
		if(result){
			_ListThuTrongTuan.Add(oThuTrongTuan);
		}
		return result;
	}
	public bool Update(){
		if(oThuTrongTuan==null)return false;
		return dao.Update(oThuTrongTuan);
	}
	public bool Delete(){
		if(oThuTrongTuan==null)return false;
		bool retvalue = dao.Delete(oThuTrongTuan);
		if(retvalue){			
			_ListThuTrongTuan.Remove(oThuTrongTuan);
		}
		return retvalue;
	}
	public bool DeleteByID(Int32 ThuTrongTuan_ID){
		bool retvalue = dao.DeleteByID(ThuTrongTuan_ID);		
		if(retvalue){
			ThuTrongTuanEntity oDeleted=FindThuTrongTuan(ThuTrongTuan_ID);
			if(oDeleted!=null){
				_ListThuTrongTuan.Remove(oDeleted);
			}
		}
		return retvalue;
	}
	public void ResetObject(){
		this.oThuTrongTuan = new ThuTrongTuanEntity();
	}
	//Check if exists MaThu
	public bool CheckExistsMaThu(object value){	
		return dao.SelectOneByMaThu(value.ToString()).MaThu!=null;
    }    
    //Copy data from data row in grid to object
    public ThuTrongTuanEntity MapDataRowToObject(DataRow row){
		ThuTrongTuanEntity o = new ThuTrongTuanEntity();
		 o.MaThu=Convert.ToString(row["MaThu"]);
		 o.TenThu=Convert.ToString(row["TenThu"]);
		 if(!row.IsNull("ThuTu"))
			o.ThuTu=ConvertInt(row["ThuTu"]);
		 o.GhiChu=Convert.ToString(row["GhiChu"]);
		 if(!row.IsNull("ThuTrongTuan_ID"))
		    o.ThuTrongTuan_ID=(Int32)(row["ThuTrongTuan_ID"]);
		 return o;
    }
	//Copy data from object to data row in grid    
    public void MapObjectToDataRow(ThuTrongTuanEntity o,DataRow row){
		row["MaThu"]=o.MaThu;
		row["TenThu"]=o.TenThu;
		row["ThuTu"]=o.ThuTu;
		row["GhiChu"]=o.GhiChu;
		row["ThuTrongTuan_ID"]=o.ThuTrongTuan_ID;
    }
    public void MapObjectToDataRow(DataRow row){
		this.MapObjectToDataRow(this.ThuTrongTuanObject,row);
    }
	public void LoadObject(Int32 ThuTrongTuan_ID){
		ThuTrongTuanEntity oLoad=FindThuTrongTuan(ThuTrongTuan_ID);
		if(oLoad==null){
			oThuTrongTuan=this.LoadThuTrongTuan(ThuTrongTuan_ID);		
			bool bNotIsNull= true;
			 bNotIsNull&=oThuTrongTuan.IsNullThuTrongTuan_ID;
			if(!bNotIsNull){
				this.ListThuTrongTuan.Add(oThuTrongTuan);
			}
		}
		else{
			oThuTrongTuan=oLoad;
		}
	}
	#region CachedList ThuTrongTuan
	private static List<ThuTrongTuanEntity> _ListThuTrongTuan=new List<ThuTrongTuanEntity>();
    public List<ThuTrongTuanEntity>ListThuTrongTuan{
		get{
			return _ListThuTrongTuan;
		}
	}
	public static List<ThuTrongTuanEntity>ListThuTrongTuanStatic{
		get{
			return _ListThuTrongTuan;
		}
	}
	public ThuTrongTuanEntity FindThuTrongTuan(Int32 ThuTrongTuan_ID){
		ThuTrongTuanEntity oResult=this.ListThuTrongTuan.Find(delegate(ThuTrongTuanEntity oFind){
			bool result=true; 
			result&=oFind.ThuTrongTuan_ID.Equals(ThuTrongTuan_ID);
			return result;
		 });
		return oResult;
	}
	public static void LoadAllThuTrongTuan(bool clearCache){
		if(_ListThuTrongTuan.Count==0)
			clearCache=true;
		if(!clearCache)return;		
		ThuTrongTuanServiceClient dao = new ThuTrongTuanServiceClient();
		_ListThuTrongTuan=new List<ThuTrongTuanEntity>(dao.SelectAll());
		dao.Close();
	}
	public ThuTrongTuanEntity LoadThuTrongTuan(Int32 ThuTrongTuan_ID){			
		ThuTrongTuanEntity o=dao.SelectOne(ThuTrongTuan_ID);		
		return o;
	}
	#endregion
}
}
